package com.atguigu.orderservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.orderservice.entity.Order;
import com.atguigu.orderservice.service.OrderService;
import com.atguigu.orderservice.service.PayLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author jyd
 * @since 2022-04-03
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduorder/payLog")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;
    //生成支付二维码接口
    @GetMapping("createNative/{orderId}")
    public R createNative(@PathVariable String orderId){
        //需要返回二维码信息和其他信息
        Map map = payLogService.createNative(orderId);
        System.out.println("返回二维码集合信息"+map);
        return R.ok().data(map);
    }
    //更新支付状态码
    @GetMapping("queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo){
        Map<String, String> map = payLogService.queryPayStatus(orderNo);
        System.out.println("**************查询订单状态*************"+map);
        if(map==null){
            return R.error().message("支付失败");
        }
        if(map.get("trade_state").equals("SUCCESS")) {
            //更改支付状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中....");
    }


}

